package com.smartx.app.model;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class TransactionModel {

    public String hash;
    public String type;
    public String from;
    public String to;
    public String value;
    public String fee;
    public String nonce;
    public String timestamp;
    public String data;
    public String gas;
    public String gasPrice;

    public static List<TransactionModel> getTransactionList(String json){
        List<TransactionModel> txList = new ArrayList<>();

        try{
            JSONObject jsonObj = new JSONObject(json);
            JSONArray jsonArr = jsonObj.optJSONArray("transactionList");

            if(jsonArr != null){
                for(int i = 0; i < jsonArr.length();i ++){
                    TransactionModel tx = new TransactionModel();
                    JSONObject jsonItem = jsonArr.getJSONObject(i);

                    tx.hash = jsonItem.optString("hash");
                    tx.type = jsonItem.optString("type");
                    tx.from = jsonItem.optString("from");
                    tx.to = jsonItem.optString("to");
                    tx.value = jsonItem.optString("value");
                    tx.fee = jsonItem.optString("fee");
                    tx.nonce = jsonItem.optString("nonce");
                    tx.timestamp = jsonItem.optString("timestamp");
                    tx.data = jsonItem.optString("data");
                    tx.gas = jsonItem.optString("gas");
                    tx.gasPrice = jsonItem.optString("gasPrice");
                    txList.add(tx);
                }
            }
            return txList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return txList;
    }

}
